// components/music/music.js
var back = wx.getBackgroundAudioManager()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    listData:Array,
    isPlay:Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    isshow:true,
    musicIndex:0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toggleMusic(){
      this.setData({ isshow: !this.data.isshow })
    },
    changeMusic(event){
      var index=event.currentTarget.dataset.index;
      var that = this;
      if (this.data.musicIndex === index) {
        return false;
      } else {
        that.setData({
          musicIndex: index,
        })
      }
      this.initMusic(index)
    },
    initMusic(musicIndex){
      console.lo
      back.src = this.data.listData[musicIndex].src
      back.title = this.data.listData[musicIndex].title // 标题为必选项
      back.play() // 开始播放
      back.onEnded(() => {
        back.src = this.data.listData[musicIndex].src
        back.title = this.data.listData[musicIndex].title // 标题为必选项
        back.play()
      })
    },
    soundControl(param){
      this.setData({ isPlay: param })
      if (this.data.isPlay) {
       back.play()
      } else {
        back.pause()
      }
    }
  }
})
